package com.huweilong.tingshu.service;

import com.huweilong.tingshu.data_object.TidToCountDO;
import com.huweilong.tingshu.data_object.TrackDO;
import com.huweilong.tingshu.repository.RelationRepo;
import com.huweilong.tingshu.repository.TrackRepo;
import com.huweilong.tingshu.util.Log;
import com.huweilong.tingshu.view_object.ListTrackVO;
import com.huweilong.tingshu.view_object.PaginationVO;
import com.huweilong.tingshu.view_object.StudioTrackListVO;
import com.huweilong.tingshu.view_object.UserVO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 是阿秋啊
 * @date 2022/07/21 09:58
 **/
public class TrackService {
    private final TrackRepo trackRepo = new TrackRepo();
    private final RelationRepo relationRepo = new RelationRepo();
    public StudioTrackListVO listOfUser(UserVO userVO , int page) {
        StudioTrackListVO resultVO = new StudioTrackListVO();
        resultVO.currentUser = userVO;
        // 0.规定每页20
        int countPerPage = 10;
        int count = trackRepo.selectCountByUid(userVO.uid);
        Log.println("属于当前用户的音频共有 " + count +" 个");
        if (count == 0) {
            resultVO.pagination = new PaginationVO();
            resultVO.pagination.currentPage = 1;
            resultVO.pagination.countPerPage = countPerPage;
            resultVO.pagination.totalPage = 1;
            resultVO.trackList = new ArrayList<>();
            return resultVO;
        }
        int totalPage = count / countPerPage;
        if (count % countPerPage > 0) {
            totalPage++;
        }
        if (page < 1) {
            page = 1;
        }
        if (page > totalPage){
            page = totalPage;
        }
        int offset = (page - 1) * countPerPage;
        // 1. 从数据库表中查询
        // 根据用户id查到用户所有的音频
        List<TrackDO> list = trackRepo.selectListByUidLimitOffset(userVO.uid,countPerPage,offset);
        Log.println("数据库表中查询到的结果："+list);
        if (!list.isEmpty()) {
            Log.println("list结果不为空,继续查询关联数量");
            // 2.根据结果,查询每个track对应的refCount(引用次数)
            List<Integer> tidList = new ArrayList<>();
            // 得到每个音频的tid
            for (TrackDO trackDO : list) {
                tidList.add(trackDO.tid);
            }
            // 在relations表中得到需要的结果Map<tid,refCount>
            Map<Integer, Integer> tidToRefCountMap = new HashMap<>();
            List<TidToCountDO> toCountDOList = relationRepo.selectCountByTidListGroupByTidList(tidList);
            for (TidToCountDO tidToCountDO : toCountDOList) {
                tidToRefCountMap.put(tidToCountDO.tid, tidToCountDO.refCount);
            }
            Log.println("tid to ref count map:"+tidToRefCountMap);

            // 3. 将TrackDO变成listTrackVO
            List<ListTrackVO> resultList = new ArrayList<>();
            for (TrackDO trackDO : list) {
                ListTrackVO view = new ListTrackVO(trackDO, tidToRefCountMap.getOrDefault(trackDO.tid, 0));
                resultList.add(view);
            }
            resultVO.trackList = resultList;
        }else {
            Log.println("list结果为空");
            resultVO.trackList=new ArrayList<>();
        }
        // 4.查询页面信息
        PaginationVO paginationVO = new PaginationVO();
        paginationVO.countPerPage = countPerPage;
        paginationVO.currentPage = page;
        paginationVO.totalPage = totalPage;
        resultVO.pagination = paginationVO;
        return resultVO;
    }
}